package com.culturalCenter.dataCenter.Utils;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成mybatis的session对象
 *
 * @author Rockylee
 */
@Slf4j
@Component
public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactor = null;
    private static SqlSession session;
    private static DruidDataSource dataSource;

    private static void init() {
        try {
            // 创建工厂
            sqlSessionFactor = sqlSessionFactory();
            // 创建session对象
            session = sqlSessionFactor.openSession(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public static SqlSessionFactory sqlSessionFactory() {
        SqlSessionFactory factory = null;
        try {
            SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
            sessionFactoryBean.setDataSource(dataSource);

            // 读取mybatis各个类的 *mapper.xml文件,这个地方的locationPattern一定要写对，不然会找不到输入的mapper.xml文件
            String locationPattern = "classpath*:/mybatis/*/*/*.xml";
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources(locationPattern);

            List<Resource> filterResourceList = new ArrayList<>();
            List<String> fileNameList = new ArrayList<>();
            for (Resource resource : resources) {
                if (!fileNameList.contains(resource.getFilename())) {
                    filterResourceList.add(resource);
                    fileNameList.add(resource.getFilename());
                }
            }

            Resource[] result = new Resource[filterResourceList.size()];
            sessionFactoryBean.setMapperLocations(filterResourceList.toArray(result));

            factory = sessionFactoryBean.getObject();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return factory;
    }

    public static SqlSession getSession() {
        if (session == null) {
            init();
        }
        return session;
    }

    public static void close() {
        if (session != null) {
            session.close();
            session = null;
        }
    }

    public static <T> T getMapper(Class<T> tClass) {
        if (session == null) {
            init();
        }
        return session.getMapper(tClass);
    }

    public static void commit() {
        if (session != null) {
            session.commit();
        }
    }

    @Autowired
    public static void setDataSource(DruidDataSource dataSource) {
        MybatisUtils.dataSource = dataSource;
    }

}
